using System;
using System.Collections.Generic;
using System.Text;
using CodeStory.Core.ObjectEntity;
using CodeStory.Core;

namespace CodeStory.Core.ObjectBusiness
{
	public sealed class UserBusiness
	{
		private UserBusiness() {

		}

		public static string RegisterUser(string userName, string password, string email, string question, string answer) {
			string msg = string.Empty;
			UserObject user = UserObject.ReadFirst("UserName=@UserName","@UserName",userName);
			if (user != null) {
				msg = string.Format("Username {0} have been used!", userName);
			} else {
				user = UserObject.New();
				user.UserID = Guid.NewGuid();
				user.UserName = userName;
				user.Password = password;
				user.PasswordQuestion = question;
				user.PasswordAnswer = answer;
				user.Email = email;
				user.RegisterDate = DateTime.Now;
				user.LastVisitedDate = DateTime.Now;
				user.LoweredUserName = userName.ToLower();
				user.Save();

				UserInRoleObject obj = UserInRoleObject.New();
				obj.UserID = user.UserID;
				obj.RoleID = new Guid(CodeStorySiteSetting.Current.DefaultRegisterUserRoleID);
				obj.Save();
			}
			return msg;
		}
	}
}
